Skip to main content

eval 示例

// 模拟的绑定对象
const bindings = {
epubIndex: "",
name: "Effective Java",
author: "Joshua Bloch"
};

// 包含占位符的脚本模板
const scriptTemplate = `
console.log("书名: ${name}");
console.log("作者: ${author}");
`;

// 使用正则表达式替换模板字符串中的占位符
function replaceBindings(template, bindings) {
return template.replace(/\$\{(\w+)\}/g, (_, key) => bindings[key]);
}

// 替换后的脚本
const script = replaceBindings(scriptTemplate, bindings);

// 执行生成的脚本
eval(script);